/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ru.vpitervip.app.client.ui;

import java.util.ArrayList;
import java.util.List;

import ru.vpitervip.app.client.place.FlatsPlace;
import ru.vpitervip.client.managed.request.FacilityProxy;
import ru.vpitervip.client.managed.request.MetroStationProxy;




import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.HasDirection.Direction;

import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.CheckBox;

/**
 * Sample implementation of {@link SearchPanel}.
 */
public class SearchPanelImpl extends Composite implements SearchPanel {

	interface Binder extends UiBinder<Widget, SearchPanelImpl> {
	}
	
	private static final Binder binder = GWT.create(Binder.class);
	@UiField IntegerBox idField;
	@UiField Button idSearchButton;
	@UiField ListBox metroComboBox;
	@UiField ListBox roomsComboBox;
	@UiField Button seachButton;
	@UiField CheckBox djakFacilityComboBox;
	@UiField CheckBox saunaFacilityComboBox;
	@UiField CheckBox condFacilityComboBox;
	@UiField CheckBox viewFacilityComboBox;

	private Presenter listener;
	
	
	private List<MetroStationProxy> metroStations;
	

	public SearchPanelImpl() {
		initWidget(binder.createAndBindUi(this));
		init();
	}

	

	private void init() {
	
		roomsComboBox.addItem("Кол-во комнат");
		roomsComboBox.addItem("1");
		roomsComboBox.addItem("2");
		roomsComboBox.addItem("3");
		roomsComboBox.addItem("4");
		roomsComboBox.addItem("5+");
		
		metroComboBox.addItem("Станция метро");
		
	}



	@Override
	public void setPresenter(Presenter listener) {
		this.listener = listener;
	}

	

	@Override
	public void setMetroStations(List<MetroStationProxy> metroStations) {
		metroComboBox.clear();
		this.metroStations=metroStations;
		metroComboBox.addItem("Станция метро");
		for (MetroStationProxy metroStationProxy:metroStations){
			metroComboBox.addItem(metroStationProxy.getRusName());
			
		}
		
		
	}
	
	
	private List<String> getSelectedFacilityIds(){
		
		List<String> facilityIds=new ArrayList<String>();
		if (djakFacilityComboBox.getValue()) {
			facilityIds.add("djak");
		}
		if (saunaFacilityComboBox.getValue()) {
			facilityIds.add("sauna");
		}
		if (condFacilityComboBox.getValue()) {
			facilityIds.add("kond");
		}
		if (viewFacilityComboBox.getValue()) {
			facilityIds.add("vid");
		}
		
		return facilityIds;
	}
	
	
	@UiHandler("idSearchButton")
	void handleIdSearchButton(ClickEvent e){
		
		Integer flatId=idField.getValue();
		
		if (flatId!=null){
			
			listener.goTo(new FlatsPlace(flatId));
		}else{
			final DecoratedPopupPanel simplePopup= new DecoratedPopupPanel(true);
			int left = idField.getAbsoluteLeft() -30;
	        int top = idField.getAbsoluteTop() + 25;
	        simplePopup.setPopupPosition(left, top);
	        simplePopup.setWidget(new HTML("<div style='color:black'>Введите число</div>"));
	        simplePopup.show();
		}
		
		
		
	}
	
	
	@UiHandler("seachButton")
	void handleSearchClicked(ClickEvent e){
		MetroStationProxy metroStation=null;
		if (metroComboBox.getSelectedIndex()>0) metroStation=metroStations.get(metroComboBox.getSelectedIndex()-1);
		Integer rooms=roomsComboBox.getSelectedIndex();
		if (rooms<=0) rooms=null;
		List<String> facilityIds=getSelectedFacilityIds();
//		System.out.println("stsrt");
		listener.goTo(new FlatsPlace(metroStation,rooms,facilityIds));
	}
	
	
	
}
